import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author XuHuaTai
 * @Date 2020/12/20  13:40
 */

public class testcode {
    public static void main(String[] args) throws IOException {
        //Define file path
        String fileName = "team.txt";

        // In order for the program to come back here, we use a loop
        while (true) {
            //main user interface
            System.out.println("-------------Welcome to the Teams manager------------");
            System.out.println("1. All teams");
            System.out.println("2. Create new team");
            System.out.println("3. Delete team");
            System.out.println("4. Update team");
            System.out.println("5. Exit");
            System.out.print("You choice:");

            //user input
            Scanner sc = new Scanner(System.in);
            String userInput = sc.nextLine();
            //Switch is used to implement selection
            switch (userInput) {
                case "1":
                    //all teams
                    findAllteams(fileName);
                    break;
                case "2":
                    //create new
                    addTeam(fileName);
                    break;
                case "3":
                    //Delete
                    deleteTeam(fileName);
                    break;
                case "4":
                    //update
                    updateTeam(fileName);
                    break;
                case "5":
                    //exit
                default:
                    System.out.println("Thanks!");
                    System.exit(0);
                    break;
            }
        }
    }

    //update team
    public static void updateTeam(String fileName) throws IOException {
        Scanner sc = new Scanner((System.in));

        ArrayList<Teams> team = new ArrayList<>();
        readFile(fileName,team);

        System.out.println("Please enter the name of the team you want to change:");
//        String teamName = sc.nextLine();
        String teamName = "test1";

        // Define an index
        int index = -1;

        //check the teamName
        for (int i = 0; i < team.size() ; i++) {
            // Get each team object
            Teams t = team.get(i);
            if (t.getName().equals(teamName)) {
                index = i;
                break;
            }
        }

        if (index == -1){
            System.out.println("Sorry,the team doesn't exist. Please check your input");
        } else {
            System.out.println("Please enter the team city you want to modify:");
//            String city = sc.nextLine();
            String city = "test";
            System.out.println("Please enter the team note you want to modify:");
//            String note = sc.nextLine();
            String note = "to be no1";
            System.out.println("Please enter the team paid you want to modify:");
//            boolean paid = sc.nextBoolean();
            boolean paid = true;
            System.out.println("Please enter the team number you want to modify:");
//            int number = sc.nextInt();
            int number = 20;

            //update information
            //create team object
            Teams t = new Teams();
            t.setName(teamName);
            t.setCity(city);
            t.setNote(note);
            t.setispaid(paid);
            t.setNumber(number);
            //update the object
            team.set(index, t);
            writeFile(fileName,team);

            System.out.println("Update successfully!");
        }
    }

    //delete
    public static void deleteTeam(String fileName) throws IOException {
        Scanner sc = new Scanner((System.in));

        ArrayList<Teams> team = new ArrayList<>();
        readFile(fileName,team);

//        System.out.println("Please enter the name of the team you want to delete:");
//        String teamName = sc.nextLine();
        String teamName = "test1";

        // Define an index
        int index = -1;

        for (int i = 0; i < team.size(); i++) {
            //Get each team object
            Teams t = team.get(i);
            if (t.getName().equals(teamName)) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            System.out.println("Sorry,the team doesn't exist. Please check your input");
        } else {
            team.remove(index);
            writeFile(fileName,team);
            System.out.println("Delete successfully!");
        }
    }

    //add
    public static void addTeam(String fileName) throws IOException {
        Scanner sc = new Scanner(System.in);
        ArrayList<Teams> team = new ArrayList<>();
        readFile(fileName,team);
        String teamName;

        //loop
        while (true) {
//            System.out.println("Please enter the name of the new team you want to create:");
            teamName = "test1";
//            teamName = sc.nextLine();

            //check
            boolean isUsed = false;
            for (Teams t : team) {
                if (t.getName().equals(teamName)) {
                    isUsed = true;
                    break;
                }
            }

            if (isUsed) {
                System.out.println("The team name you entered is already occupied. Please re-enter it");
            } else {
                break;
            }
        }

//        System.out.println("Please enter the team city:");
//        String city = sc.nextLine();
        String city = "test";
//        System.out.println("Please enter the team note:");
//        String note = sc.nextLine();
        String note = "asd";
//        System.out.println("Please enter the team isPaid:");
//        boolean paid = sc.nextBoolean();
        boolean paid = true;
//        System.out.println("Please enter the team number:");
//        int number = sc.nextInt();
        int number = 20;

        //add information
        Teams t = new Teams();
        t.setName(teamName);
        t.setCity(city);
        t.setNote(note);
        t.setispaid(paid);
        t.setNumber(number);
        team.add(t);
        writeFile(fileName,team);

        System.out.println("Create successfully!");
    }

    //find
    public static void findAllteams(String fileName) throws IOException {
        //create the object
        Teams t;
        ArrayList<Teams> team = new ArrayList<>();

        readFile(fileName,team);

        if (team.size() == 0) {
            System.out.println("Sorry, there is no team information available, please select your operation again");
        } else {
            System.out.println("Name\t\tcity\t\tnote\t\tisPaid\t\tnumber");
            for (Teams teams : team) {
                t = teams;
                System.out.println(t.getName() + "\t\t\t" + t.getCity() + "\t\t\t" + t.getNote() + "\t\t" + t.getisPaid() + "\t\t" + t.getNumber());
            }
        }
    }

    //readFile
    public static void readFile(String fileName, ArrayList<Teams> team) throws IOException{
        //create IOE
        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);

        String line;
        while ((line = br.readLine()) != null) {
            String[] data = line.split(",");
            Teams t = new Teams();
            t.setName(data[0]);
            t.setCity(data[1]);
            t.setNote(data[2]);
            t.setispaid(Boolean.parseBoolean(data[3]));
            t.setNumber(Integer.parseInt(data[4]));

            team.add(t);
        }
        br.close();
    }

    public static void writeFile(String fileName, ArrayList<Teams> team) throws IOException {
        //create IOE
        FileWriter fw = new FileWriter(fileName);
        BufferedWriter bw = new BufferedWriter(fw);

        for (Teams t : team) {
            bw.write(t.getName() + "," + t.getCity() + "," +
                    t.getNote() + "," + t.getisPaid() + "," +
                    t.getNumber());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }

}
